package com.alibaba.citrus.ots.price.ability.functions.entity.activity;

import com.epoch.app.otspricecenter.dto.request.ParticipateActivityInfoRequest;
import com.epoch.app.otspricecenter.dto.response.ParticipateActivityInfoResponse;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Map;
import java.util.Objects;

/**
 * @author hanguo
 * @date 2021/12/2
 * @Description 用于参加活动时聚合数据
 */
@Data
public class ActivityTO {

    /**
     * 活动号
     */
    private String activityId;

    /**
     * 活动项目号
     */
    private String activityTermId;

    /**
     * 促销活动分组管控标识
     */
    private Boolean groupControlFlag;

    /**
     * 请求参数和活动实体对应关系
     */
    private Map<ParticipateActivityInfoRequest,ActivityEntity> activityInfoRequestActivityEntityMap;

    /**
     * 请求参数和预占结果的对应关系
     */
    private Map<ParticipateActivityInfoRequest, ParticipateActivityInfoResponse> activityInfoResponseMap;

    /**
     * 请求参数和更新结果的对应关系
     */

    /**
     * 该活动项目总占用数量
     */
    private Integer quantitySum;

    /**
     * 活动占用费用
     */
    private Long feeSum;

    /**
     * 活动占用结果
     */
    private Boolean result;

    /**
     * 活动便签号
     */
    private String activityNoteId;

    /**
     * 是否可以占用数量和占用金额成功
     * @return
     */
    public Boolean isCanOccupy() {
        ActivityEntity activityEntity = this.getActivityInfoRequestActivityEntityMap().values().stream().findFirst().orElse(null);

        if (!groupControlFlag) {

            ActivityTermEntity termEntity = null;
            if (CollectionUtils.isNotEmpty(activityEntity.getTermEntityList())) {
                termEntity = activityEntity.getTermEntityList().stream().filter(
                        e -> this.getActivityTermId().equals(e.obtainActivityTermId())).findFirst().orElse(null);
            }

            if (Objects.isNull(termEntity)) {
                return false;
            }
            return termEntity.isCanOccupyTerm(this.getQuantitySum(), this.getFeeSum());
        } else {

            ActivityTitleExtEntity activityTitleExtEntity = activityEntity.getTitleExtEntity();

            if (Objects.isNull(activityTitleExtEntity)) {
                return false;
            }

            return activityTitleExtEntity.isCanOccupyTerm(this.getQuantitySum(), this.getFeeSum());
        }

    }
}
